package com.mingsoft.led;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mingsoft.led.camera.CameraBean;
import com.mingsoft.led.camera.CameraConfig;
import com.mingsoft.led.comm.CommQueue;
import com.mingsoft.led.util.Configure;

@Controller
public class LedMainController {

	@Autowired
	@Qualifier("statusBean")
	private StatusBean statusBean = null;

	@Autowired
	@Qualifier("labelBean")
	private LabelBean labelBean = null;

	@RequestMapping(value = "/")
	public String login(ModelMap model, HttpSession session) {
		model.addAttribute("labelBean", labelBean);
		session.removeAttribute("###LOGGED IN###");
		return "login";
	}

	@RequestMapping(value = "/home")
	public String home(ModelMap model, HttpSession session) {
		model.addAttribute("labelBean", labelBean);
		FileBean.getInstance().update();
		if (session.getAttribute("###LOGGED IN###") == null) {
			return "login";
		}
		model.addAttribute("statusBean", statusBean);
		model.addAttribute("fileBean", FileBean.getInstance());
		model.addAttribute("cameraBean", CameraBean.getInstance());

		return "home";
	}

	@RequestMapping(value = "/config")
	public String config(ModelMap model, HttpSession session) {
		model.addAttribute("labelBean", labelBean);
		FileBean.getInstance().update();
		if (session.getAttribute("###LOGGED IN###") == null) {
			return "login";
		}
		model.addAttribute("statusBean", statusBean);
		model.addAttribute("fileBean", FileBean.getInstance());
		model.addAttribute("cameraBean", CameraBean.getInstance());
		model.addAttribute("fileUploadUrl",
				Configure.getInstance().get(Configure.KEY_FILE_MANAGEMENT_URL));
		model.addAttribute("welcomePath",
				Configure.getInstance().get(Configure.KEY_WELCOME_PATH));
		model.addAttribute("rootPath",
				Configure.getInstance().get(Configure.KEY_ROOT_PATH));

		String str = null;
		for (int i = 0; i < 6; ++i) {
			if (null != (str = Configure.getInstance().get(
					Configure.KEY_CAMERA_CFG + i))) {
				CameraConfig cc = CameraConfig.parse(str);
				if (cc != null) {
					model.addAttribute("cameraName" + i, cc.getName());
					model.addAttribute("cameraIP" + i, cc.getIp());
					model.addAttribute("cameraPort" + i, cc.getPort());
					model.addAttribute("cameraUserName" + i, cc.getUsername());
					model.addAttribute("cameraPassword" + i, cc.getPassword());
					model.addAttribute("cameraChannel" + i, cc.getChannel());
				}
			}
		}

		return "admin/config";
	}

	@RequestMapping(value = "/statusUpdate", method = RequestMethod.POST)
	public String statusUpdate(ModelMap model) {
		model.addAttribute("statusBean", statusBean);
		return "statusUpdateJSON";
	}

	@RequestMapping(value = "/changeCamera", method = RequestMethod.POST)
	public String changeCamera(@RequestParam("cameraName") String cameraName,
			@RequestParam("cameraIP") String cameraIP,
			@RequestParam("cameraPort") String cameraPort,
			@RequestParam("cameraUserName") String cameraUserName,
			@RequestParam("cameraPassword") String cameraPassword,
			@RequestParam("cameraChannel") String cameraChannel,
			@RequestParam("cameraId") String cameraId, ModelMap model) {
		if (cameraName.length() == 0) {
			if (Configure.getInstance().set(
					Configure.KEY_CAMERA_CFG + cameraId, null)) {
				model.addAttribute("succeed", true);
				model.addAttribute("message", "摄像头参数修改成功");
				CameraBean.getInstance().update();
			} else {
				model.addAttribute("succeed", false);
				model.addAttribute("message", "摄像头参数修改失败");
			}
		} else if (Configure.getInstance().set(
				Configure.KEY_CAMERA_CFG + cameraId,
				cameraName + "{" + cameraIP + "{" + cameraPort + "{"
						+ cameraUserName + "{" + cameraPassword + "{"
						+ cameraChannel)) {
			model.addAttribute("succeed", true);
			model.addAttribute("message", "摄像头参数修改成功");
			CameraBean.getInstance().update();
		} else {
			model.addAttribute("succeed", false);
			model.addAttribute("message", "摄像头参数修改失败");
		}
		return "admin/configChangeJSON";
	}

	@RequestMapping(value = "/resetWelcomePath", method = RequestMethod.POST)
	public String resetWelcomePath(
			@RequestParam("welcomePath") String welcomePath, ModelMap model) {
		if (Configure.getInstance()
				.set(Configure.KEY_WELCOME_PATH, welcomePath)) {
			model.addAttribute("succeed", true);
			model.addAttribute("message", "欢迎界面路径修改成功");
		} else {
			model.addAttribute("succeed", false);
			model.addAttribute("message", "欢迎界面路径修改失败");
		}
		return "admin/configChangeJSON";
	}

	@RequestMapping(value = "/resetRootPath", method = RequestMethod.POST)
	public String resetRootPath(@RequestParam("rootPath") String rootPath,
			ModelMap model) {
		if (Configure.getInstance().set(Configure.KEY_ROOT_PATH, rootPath)) {
			model.addAttribute("succeed", true);
			model.addAttribute("message", "根路径修改成功");
		} else {
			model.addAttribute("succeed", false);
			model.addAttribute("message", "根路径修改失败");
		}
		return "admin/configChangeJSON";
	}

	@RequestMapping(value = "/changeFileUploadUrl", method = RequestMethod.POST)
	public String changeFileUploadUrl(
			@RequestParam("fileUploadUrl") String fileUploadUrl, ModelMap model) {
		if (Configure.getInstance().set(Configure.KEY_FILE_MANAGEMENT_URL,
				fileUploadUrl)) {
			model.addAttribute("succeed", true);
			model.addAttribute("message", "文件上传路径修改成功");
		} else {
			model.addAttribute("succeed", false);
			model.addAttribute("message", "文件上传路径修改失败");
		}
		return "admin/configChangeJSON";
	}

	@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
	public String changePassword(
			@RequestParam("currentPassword") String currentPassword,
			@RequestParam("newPassword") String newPassword,
			@RequestParam("confirmPassword") String confirmPassword,
			ModelMap model) {
		model.addAttribute("succeed", false);
		if (currentPassword.equals(Configure.getInstance().get(
				Configure.KEY_ADMIN_PWD))) {
			if (newPassword.equals(confirmPassword)) {
				if (Configure.getInstance().set(Configure.KEY_ADMIN_PWD,
						newPassword)) {
					model.addAttribute("succeed", true);
					model.addAttribute("message", "密码修改成功");
				} else {
					model.addAttribute("message", "密码修改失败");
				}
			} else {
				model.addAttribute("message", "两次密码输入不匹配");
			}
		} else {
			model.addAttribute("message", "旧密码输入错误");
		}
		return "admin/configChangeJSON";
	}

	@RequestMapping(value = "/command", method = RequestMethod.POST)
	public String command(@RequestParam("currentNav") String currentNav,
			@RequestParam("controllerOn") boolean controllerOn,
			@RequestParam("changeNavTo") String changeNavTo,
			@RequestParam("changeControllerTo") boolean changeControllerTo,
			@RequestParam("commands") String commands,
			@RequestParam("param") String param, ModelMap model) {
		if (statusBean.getCurrentNav().getId().equals(currentNav)
				&& statusBean.isControllerOn() == controllerOn) {
			statusBean.setCurrentNav(Navigations.getNavigation(changeNavTo));
			statusBean.setControllerOn(changeControllerTo);

			Cmd cmd = statusBean.generateCmd(commands, param);
			CommQueue.getInstance().addCommand(cmd);

			if (cmd.getCmd() == Commands.VIDEO_OPEN
					|| cmd.getCmd() == Commands.VIDEO_PLAY) {
				statusBean.setVideoPaused(false);
			} else if (cmd.getCmd() == Commands.VIDEO_PAUSE) {
				statusBean.setVideoPaused(true);
			}

			model.addAttribute("succeed", true);
		} else {
			model.addAttribute("succeed", false);
		}
		model.addAttribute("statusBean", statusBean);
		return "statusChangeJSON";
	}
}
